[Leetcode46] Permutation 全排列
“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.”
Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。
`
java
/**
全排列
*/
public class leetcode46 {
public List<List> permute (int[] nums) { List<List<Integer>> res = new ArrayList<>(); helper(res, nums, 0); return res;
}
private void helper (List<List
> res, int[] nums, int pos) { if (pos == nums.length) { List<Integer> list = new ArrayList<>(); for (int t : nums) { list.add(t); } res.add(list); } for (int i = pos; i < nums.length; i++) { swap(nums, pos, i); helper(res, nums, pos + 1); swap(nums, pos, i); }
}
private void swap (int[] nums, int i, int j) {
int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;
}
}
`
**